#include "debug.h"
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include "Utils\\ResGroupManager.h"
#include "Utils\\AWindow.h"

NS_BEGIN(SDL)
NS_BEGIN(UTILS)

#pragma region CTOR & DTOR

AWindow::AWindow()
	: m_screen(NULL)
{
	CTOR("AWindow", this);
}

AWindow::AWindow(AWindow const&)
{
}

AWindow::~AWindow()
{
	TTF_Quit();
	SDL_Quit();
}

#pragma endregion
#pragma region Operators

AWindow& AWindow::operator=(AWindow const&)
{
	return (*this);
}

#pragma endregion

bool AWindow::InitLibs(SDLConfig const& _sdlConfig)
{
	if (AWindow::InitSDL(_sdlConfig) == false)
		return (false);
	if (AWindow::InitTTF() == false)
		return (false);
	return (true);
}

bool AWindow::Init(SDLConfig const& _sdlConfig)
{
	SDL_WM_SetCaption(_sdlConfig.Title.c_str(), NULL);
	SDL_Surface* icon = IMG_Load(_sdlConfig.IconPath.c_str());
	if (icon != NULL)
	{
		SDL_SetColorKey(icon, SDL_SRCCOLORKEY, 0xFF00FF);
		SDL_WM_SetIcon(icon, NULL);
	}

	m_screen = SDL_SetVideoMode(_sdlConfig.Width, _sdlConfig.Height, 32,
								_sdlConfig.VideoFlags);

	ResGroup* res = ResGroupManager::Instance()[ResGroupManager::DEFAULT_GROUP];
	res->AddResource("PushButton_released", "PushButton_released.png");
	res->AddResource("PushButton_hovered", "PushButton_hovered.png");
	res->AddResource("PushButton_pressed", "PushButton_pressed.png");
	res->AddResource("PushButton_clicked", "PushButton_clicked.png");
	res->AddResource("PushButton_disabled", "PushButton_disabled.png");
	
	res->AddResource("CheckBox_released", "CheckBox_released.png");
	res->AddResource("CheckBox_hovered", "CheckBox_hovered.png");
	res->AddResource("CheckBox_pressed", "CheckBox_pressed.png");
	res->AddResource("CheckBox_clicked", "CheckBox_clicked.png");
	res->AddResource("CheckBox_disabled", "CheckBox_disabled.png");
	res->AddResource("CheckBox_checked", "checked.png");
	return (true);
}

bool AWindow::InitSDL(SDLConfig const& _sdlConfig)
{
	SDL_putenv(_sdlConfig.EnvParams.c_str());
	if (SDL_Init(_sdlConfig.SDLFlags) == -1)
	{
		FATAL_ERR(SDL_GetError());
		return (false);
	}
	return (true);
}

bool AWindow::InitTTF()
{
	return (TTF_Init() != -1);
}

NS_END(UTILS)
NS_END(SDL)
